<?php
/**
 * 匹配成对的标记 可以嵌套匹配
 */
$br = "<br/>\n";
$br2 = str_repeat($br, 2);

$newS = $s = "([[{}]])[]";

do {
    $newT = $newS;
    $newS = str_replace(['[]', '{}','()']
    , ''
    , $newS
    );
} while($newS != $newT && '' != $newS);

var_dump($newS);

// 循环直到字符串中不再包含任何匹配的成对标记
//$patterns = ['/\(\)/', '/\[\]/', '/{}/'];
$patterns = '/\(\)|/\[\]|{}/'; //"|"替换符的优先级低于字符,所以用|分割的字符串不需要包含起来
do {
    $newS = $s;
    $s = preg_replace($patterns, '', $s);
    echo $s, $br2;
} while ($newS !== $s && '' != $s);

var_dump($s); // 输出: string(0) ""




echo '中文将中间字符替换为*', $br;

$nameArr = ['李萌的小光', '死了都要爱', '爱', '简爱', '简单爱'];
$replace = '*';
foreach ($nameArr as &$name) {
  if (3 < mb_strlen($name)) {
    $name = mb_substr($name, 0, 1) . '*' . mb_substr($name, -1);
  } else {
    $name = '*' . mb_substr($name, -1);
  }
}
var_dump($nameArr);


echo $br2;

//非中文字符串将中间字符替换为*
echo '非中文字符串将中间字符替换为*', $br;
$str = substr_replace('', '****', 3, 4);
var_dump($str);

echo $br2;

echo '替换字符串中的指定数字序列', $br;
$string = '光天使:战斗阶段提升2000攻击力5%的暴击率';
$arr =[8000, 20];
$pattern = '/(\d+)(.*?)(\d+)/i';
$replacement = $arr[0].'${2}' . $arr[1];
echo preg_replace($pattern, $replacement, $string);

echo $br;



echo '正则替换指定的信息', $br;
$string = '力天使:每次掷骰子增加5000的固定伤害';
 $pattern = '/\d+/';
$replacement = 3000;
echo preg_replace($pattern, $replacement, $string, 1);



$res = preg_replace('/a|b|c/', '', ['abc']);
var_dump($res);










